package edu.tsinghua.lumaqq.demo;

import edu.tsinghua.lumaqq.qq.Util;
import edu.tsinghua.lumaqq.qq._08QQClient;
import edu.tsinghua.lumaqq.qq.beans.QQUser;
import edu.tsinghua.lumaqq.qq.events.IQQListener;
import edu.tsinghua.lumaqq.qq.events._08QQEvent;
import edu.tsinghua.lumaqq.qq.net.PortGateFactory;

public class JamesQQ implements IQQListener {
	private final static int QQID = 425059372;
	private final static String QQPWD = "jamesjames";
	private final static int RECEIVER = 289156401;
	private final static String WELCOME_STR = "hello? I'am online now!";
	private final static String SERVER = "58.251.62.60";
	
	private _08QQClient client;
	
	private void init(){
		client = new _08QQClient();
		client.setConnectionPoolFactory(new PortGateFactory());
        QQUser user = new QQUser(QQID, QQPWD);
        user.setUdp(true);
        client.setUser(user);
        client.addQQListener(this);
        client.setLoginServer(SERVER);
        try {
            client.sendTouch();
            Thread.sleep(10000);
            client.im_Send(RECEIVER, Util.getBytes(WELCOME_STR));
            Thread.sleep(6000);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
	}
	
	@Override
	public void qqEvent(_08QQEvent e) {
		System.out.println("an event occour!" + e.toString());
		if(e.type == _08QQEvent.IM_RECEIVED){
			String msg = client.getMessage(e).trim();
			System.out.println("message: " + msg);
			client.im_Send(RECEIVER, Util.getBytes("hi"));
			if(msg.equals("hello?")){
				client.im_Send(RECEIVER, Util.getBytes("yes? I'm still online!"));
			}
			if(msg.equals("exit")){
				System.out.println("=====exiting=====");
				client.logout();
				client.release();
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new JamesQQ().init();
	}

}
